<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="所属工厂">
        <el-select v-model="queryParams.factoryId" placeholder="请选择">
          <el-option
            v-for="item in factoryOptions"
            :key="item.id"
            :label="item.factoryName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属车间">
        <el-select v-model="queryParams.workshopId" placeholder="请选择">
          <el-option
            v-for="item in workshopOptions"
            :key="item.id"
            :label="item.workshopName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属网关">
        <el-select v-model="queryParams.edgeId" placeholder="请选择">
          <el-option
            v-for="item in edgeOptions"
            :key="item.id"
            :label="item.edgeName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceSerialNumber">
        <el-input
          v-model="queryParams.deviceSerialNumber"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['imms:alarmRecord:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['imms:alarmRecord:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['imms:alarmRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['imms:alarmRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属工厂名称" align="center" prop="factoryId" >
        <template scope="scope">
          <span >{{ getFactoryName(scope.row.factoryId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属车间名称" align="center" prop="workshopId" >
        <template scope="scope">
          <span >{{ getWorkshopName(scope.row.workshopId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属网关名称" align="center" prop="edgeId" >
        <template scope="scope">
          <span >{{ getEdgeName(scope.row.edgeId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备编号" align="center" prop="deviceSerialNumber" />
      <el-table-column label="报警时间" align="center" prop="edgeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.edgeTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警信息" align="center" prop="content" />
      <el-table-column label="报警状态" align="center" prop="edgeTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.status === '0'" style="color: #ffba00">处理中</span>
          <span v-else style="color: #2aa515">已处理</span>
        </template>
      </el-table-column>
      <el-table-column label="处理信息记录" align="center" prop="comment" :show-overflow-tooltip='true' />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['imms:alarmRecord:edit']"
          >处理完成</el-button>
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['imms:alarmRecord:edit']"
          >查看记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['imms:alarmRecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报警记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" disabled/>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceSerialNumber">
          <el-input v-model="form.deviceSerialNumber" placeholder="请输入设备编号" disabled/>
        </el-form-item>
        <el-form-item label="维护记录" prop="comment">
          <el-input
            type="textarea"
            v-model="form.comment"
            placeholder="若有其他信息或建议需要记录，在此输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看报警记录 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" disabled/>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceSerialNumber">
          <el-input v-model="form.deviceSerialNumber" placeholder="请输入设备编号" disabled/>
        </el-form-item>
        <el-form-item label="维护记录" prop="comment">
          <el-input
            disabled
            type="textarea"
            v-model="form.comment"
            placeholder="若有其他信息或建议需要记录，在此输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">退出</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAlarmRecord,
  delAlarmRecord,
  addAlarmRecord,
  updateAlarmRecord,
  pageAlarmRecord
} from "@/api/imms/alarmRecord";
import {listWorkshop} from "@/api/imms/workshop";
import {listEdge} from "@/api/imms/edge";
import {listFactory} from "@/api/imms/factory";

export default {
  name: "AlarmRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警记录表格数据
      alarmRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryId: null,
        workshopId: null,
        edgeId: null,
        deviceName: null,
        deviceSerialNumber: null,
        edgeTime: null,
      },
      factoryOptions:[],
      workshopOptions:[],
      edgeOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getFactoryList();
    this.getWorkshopList();
    this.getEdgeList();
  },
  methods: {
    /** 查询报警记录列表 */
    getList() {
      this.loading = true;
      pageAlarmRecord(this.queryParams).then(response => {
        this.alarmRecordList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询工厂下拉列表 */
    getFactoryList() {
      listFactory().then(response => {
        this.factoryOptions = response.data
      })
    },
    /** 查询车间下拉列表 */
    getWorkshopList() {
      listWorkshop().then(response => {
        this.workshopOptions = response.data
      })
    },
    /** 查询网关下拉列表 */
    getEdgeList() {
      listEdge().then(response => {
        this.edgeOptions = response.data
      })
    },
    getFactoryName(factoryId){
      const factory = this.factoryOptions.find(item => item.id === factoryId);
      return factory ? factory.factoryName : '';
    },
    getWorkshopName(workshopId){
      const workshop = this.workshopOptions.find(item =>item.id === workshopId);
      return workshop ? workshop.workshopName : '';
    },
    getEdgeName(edgeId){
      const edge = this.edgeOptions.find(item =>item.id === edgeId);
      return edge ? edge.edgeName : '';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryId: null,
        workshopId: null,
        edgeId: null,
        deviceName: null,
        deviceSerialNumber: null,
        edgeTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        content: null,
        status: null,
        comment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAlarmRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "处理报警记录";
      });
    },
    /** 查看按钮操作*/
    handleView(row){
      this.reset();
      const id = row.id || this.ids
      getAlarmRecord(id).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "查看报警记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.status = '1'
            updateAlarmRecord(this.form).then(response => {
              this.$modal.msgSuccess("确认成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarmRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报警记录编号为"' + ids + '"的数据项？').then(function() {
        return delAlarmRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('imms/alarmRecord/export', {
        ...this.queryParams
      }, `alarmRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
